<!DOCTYPE html> <html> <head> <style> </style> <script> </script> </head> <body> <script> </script> </body> <script> </script> </html> <script> </script>
<script>
and </script>
; Multiple scripts share the same memory space.{ ... }
var
, let
, const
;
Let's not use var
variables except when you need them for testing.
Let's use let
. (Note that you cannot redeclare the same let variables.)
{ const PI = 3.14159; let radius = prompt("Enter a number: "); // or window.prompt(...) let circumference; circumference = 2 * PI * radius; alert(circumference); // or window.alert(...) }
alert()
.
%
, **
+
let name = prompt("Enter a name: "); let message = "Welcome " + name + "!"; alert(message);
.
document.getElementById('modal-login').style.display = 'block';
window
object.
window.promt()
or just promt()
window.alert()
or just alert()
window.document
or just document
document
.
document.getElementById("target").style.backgroundColor = "Cyan";
alert(document.getElementById("target").innerHTML);
<button id='target'>Click me!</button> <div id='display'>Message here</div> <script> document.getElementById("target").addEventListener("click", function() { // "click" event listener document.getElementById("display").innerHTML = "Interesting!"; }); </script>
%
operator.
// These are both globals. let foo = 1; var bar = 2; function test() { let foo = 1; // Local bar = 1; // Global // Execute an anonymous function. Isn't it interesting? (function() { let wibble = 1; // Local foo = 3; // Inherits from scope above (creating a closure) moo = 3; // Global??? You should be careful with this. })(); alert(foo); alert(bar); alert(moo); alert(wibble); } test();
window.carName
, in the above link.
person[2] = 'Dave';
.
What do you see?
Linear arrays | Objects (associative arrays) |
---|---|
const x = []; | const o = {}; |
x[2] = 'Wow!'; | o.today = 'Friday'; o['today'] = 'Saturday'; let p = "today"; o[p] = "Monday"; |
delete x[1]; | delete o.another; |
for(let i = 0; i < x.length; i++) | for (let p in o) |